#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
// #include <E:\Desktop\data_struct\12Lab\rand_.h>
typedef struct even
{
	int id;   //车辆唯一编号
	int time; //时间发生时间
	int type; //时间类型  --0 到达 --1 离开
	struct even *next;
};
// 营业时间
const int bus_time = 600;
// 事件链表
even *evlist;
// 停车场栈
even *park1[20], *park2[100];
const int park_content = 12;
int top = -1;
int top2 = -1;
// 总共车辆数
int count = -1;
even *ev;
// 停车价格 每个单位时间10元
const int price = 10;

int rand_(int seed_)
{
	time_t t = time(NULL);
	srand(t);
	return rand() % seed_;
}

// 插入到事件表中 按时间顺序排列
void insert(even *&node)
{
	even *t, *pre;
	t = evlist;
	while (t && node->time > t->time)
	{
		pre = t;
		t = t->next;
	}
	node->next = t;
	pre->next = node;
}

void inital_park()
{
	evlist = (even *)malloc(sizeof(even));
	evlist->next = NULL;
	evlist->time = -1;
	even *arv = (even *)malloc(sizeof(even));
	arv->id = ++count;
	arv->time = 0;
	arv->type = 0;
	arv->next = evlist->next;
	evlist->next = arv;
}

void Arrive()
{
	printf("car:%d already arrive time--%d\n", ev->id, ev->time);
	even *dp, *ar;
	dp = (even *)malloc(sizeof(even));
	ar = (even *)malloc(sizeof(even));
	int dur_t, next_t;
	// 随机数的选取范围很重要 ！！！
	dur_t = rand_(97);
	next_t = rand_(7);
	/************************ */
	dp->id = ev->id;
	dp->time = ev->time + dur_t;
	dp->type = 1;
	insert(dp);
	ar->id = ++count;
	ar->time = ev->time + next_t;
	ar->type = 0;
	if (ar->time < bus_time)
		insert(ar);
	if (top < park_content)
	{
		park1[++top] = ev;
		printf("now park1 has %d cars\n", top);
	}
	else
	{
		park2[++top2] = ev;
		printf("now park2 has %d cars\n", top2);
	}
}
int search_car(even *park[], int park_top)
{
	int i = 0;
	while (i <= park_top)
	{
		if (park[i]->id == ev->id)
			return i;
		i++;
	}
	return -1;
}

// 车辆出栈 先把车辆后面的车子全部出栈并入栈到park2 -- 再把park2 中的车子放入park1
//mark 这里容易出错
void exit_car(int car_pos)
{
	for (int i = car_pos; i < top; i++)
	{
		park1[i] = park1[i + 1];
	}
	if (park2[0])
	{
		park1[top] = park2[0];
		for (int j = 0; j < top2; j++)
		{
			park2[j] = park2[j + 1];
		}
		top2--;
	}
	else
		top--;
}
void Depart()
{
	int car_pos = search_car(park1, top);
	if (car_pos != -1)
	{
		even *ev_car = park1[car_pos];
		int dur = ev->time - ev_car->time;
		int cost = dur * price;
		printf("exit from park1->car_id=%d,<%d-%d>-->cost=%d\n", ev->id, ev_car->time, ev->time, cost);
		exit_car(car_pos);
		printf("now park1 has %d cars\n", top);
	}
	else
	{
		car_pos = search_car(park2, top2);
		even *ev_car = park2[car_pos];
		for (int i = car_pos; i < top2; i++)
		{
			park2[i] = park2[i + 1];
		}
		top2--;
		printf("exit from park2->car_id=%d,<%d-%d>-->cost=0\n", ev->id, ev_car->time, ev->time);
		printf("now park2 has %d cars\n", top2);
	}
}
int main()
{
	/*******************code*******************/
	inital_park();
	while (evlist->next)
	{
		// 出队一个元素
		ev = evlist->next;
		evlist->next = ev->next;
		Sleep(500);
		if (ev->type == 0)
			Arrive();
		else
			Depart();
	}
	/******************************************/
	printf("\n\n****************************\n");
	printf("Press Enter key to continue\n");
	getchar();
	return 0;
	/******************************************/
}
